<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            height: 2000px;
        }
    </style>
</head>
<body>
    <script>
        //滚动时获取距离顶部的高度
        window.onscroll = function(){
            //1.
            var height = document.documentElement.scrollTop;
            console.log(height);
            //2.
            var height = window.scrollY;
            console.log(height);
        }
    </script>
</body>
</html>